/*----------------------------------
PRE-PROCESSING: Analysis codes for:
A Registered Report on Place-Based Resentment: Exploring Urban-Rural Tensions in Sweden
------------------------------------*/

clear all // Reset memory and datasets

// Set working directory
cd "______"

// Import raw dataset from Excel (first row contains variable names)
import excel "_____RawData.xlsx", sheet("Sheet0") firstrow


// -------------------------------------------------------------
// DEFINE VARIABLES
// (See codebook; Rural_ID_Bin: 2 = rural, 1 = urban)
// -------------------------------------------------------------

// 1. PLACE-BASED IDENTITY STRENGTH
// Mean of identity strength items, calculated separately for rural and urban respondents
gen ID_strength=.
replace ID_strength=(ID1_rural+ID2_rural+ID3_rural+ID4_rural)/4 if Rural_ID_Bin==2
replace ID_strength=(ID1_urban+ID2_urban+ID3_urban+ID4_urban)/4 if Rural_ID_Bin==1

// 2. PLACE-BASED RESENTMENT SCORE
// Mean of resentment items, separately for rural and urban respondents
gen resent=.
replace resent=(Resent_rural1+Resent_rural2+Resent_rural3+Resent_rural4)/4 if Rural_ID_Bin==2
replace resent=(Resent_urban1+Resent_urban2+Resent_urban3+Resent_urban4)/4 if Rural_ID_Bin==1

// 3. PLACE-BASED TRAIT EVALUATION INDEX (PTEI)
// Measure of relative evaluation of rural vs. urban traits (see Table 2 in paper)
// Scale ranges from –6 (ingroup more negative) to +6 (outgroup more negative)

// Reverse-code positive traits (7-point scale → reverse into negative evaluation)
gen Rur_honestR = 8 - Rur_honest
gen Urb_honestR = 8 - Urb_honest
gen Rur_intelR  = 8 - Rur_intel
gen Urb_intelR  = 8 - Urb_intel

// Compute average trait score for rural (ruralTherm) and urban (urbanTherm) targets
gen ruralTherm = (Rur_honestR + Rur_intelR + Rur_self + Rur_prejud)/4
gen urbanTherm = (Urb_honestR + Urb_intelR + Urb_self + Urb_prejud)/4

// Compute relative evaluation index: outgroup – ingroup
gen PTEI=.
replace PTEI = urbanTherm - ruralTherm if Rural_ID_Bin==2 // Rural respondents
replace PTEI = ruralTherm - urbanTherm if Rural_ID_Bin==1 // Urban respondents


// -------------------------------------------------------------
// POLICY AGREEMENT VARIABLES
// Each respondent is randomly assigned one of three policies,
// presented by either a rural or urban politician
// -------------------------------------------------------------

// Education policy
gen EducPol=.
replace EducPol=Educ_Rural if Educ_Rural!=.
replace EducPol=Educ_Urban if Educ_Urban!=.

// Immigration policy
gen ImmigPol=.
replace ImmigPol=Immig_Rural if Immig_Rural!=.
replace ImmigPol=Immig_Urban if Immig_Urban!=.

// Health policy
gen HealthPol=.
replace HealthPol=Health_Rural if Health_Rural!=.
replace HealthPol=Health_Urban if Health_Urban!=.

// Combined "policy" variable: pooled agreement across all proposals
gen policy=.
replace policy=EducPol if EducPol!=.
replace policy=ImmigPol if ImmigPol!=.
replace policy=HealthPol if HealthPol!=.


// -------------------------------------------------------------
// EXPERIMENTAL CONDITION
// Indicator for politician cue (1 = rural, 0 = urban)
// -------------------------------------------------------------

gen ruralPolitician=.
replace ruralPolitician=1 if Educ_Rural!=.
replace ruralPolitician=0 if Educ_Urban!=.
replace ruralPolitician=1 if Immig_Rural!=.
replace ruralPolitician=0 if Immig_Urban!=.
replace ruralPolitician=1 if Health_Rural!=.
replace ruralPolitician=0 if Health_Urban!=.


// -------------------------------------------------------------
// COVARIATES
// Recoded according to pre-registration
// -------------------------------------------------------------

// Education (binary: 1 = university-level, 0 = less than university)
gen Uni=.
replace Uni=1 if Educ==6
replace Uni=1 if Educ==5
replace Uni=0 if Educ==4
replace Uni=0 if Educ==3
replace Uni=0 if Educ==2
replace Uni=0 if Educ==1

// Gender (binary: male = 1; female/other = 0)
gen male=.
replace male=1 if Gender==1
replace male=0 if Gender==2
replace male=0 if Gender==3

// Urbanicity (1 = rural area, 5 = big city)
gen bigCity=.
replace bigCity=1 if Geo==5
replace bigCity=2 if Geo==4
replace bigCity=3 if Geo==3
replace bigCity=4 if Geo==2
replace bigCity=5 if Geo==1

// Rural/Urban group indicators (T1 = rural, T2 = urban)
gen T1=0 
replace T1=1 if Rural_ID_Bin==2 
gen T2=0 
replace T2=1 if Rural_ID_Bin==1


// -------------------------------------------------------------
// LABEL VARIABLES
// -------------------------------------------------------------

label variable ruralPolitician "Rural politician"
label define condition_lab 0 "Urban Politician" 1 "Rural Politician"
label values ruralPolitician condition_lab

label define cities 1 "Rural area" 2 "Small city" 3 "Medium-sized city" 4 "Suburb" 5 "Big city"
label values bigCity cities

label variable Uni "University education"
label variable ID_strength "Identity strength"
label variable resent "Place resentment"
label variable PTEI "Trait evaluation index"
label variable Pol_Right "Political orientation"


// -------------------------------------------------------------
// EXCLUSION OF PARTICIPANTS (as per pre-registration)
// -------------------------------------------------------------

// Drop respondents who did not complete full survey (missing on last question: Pol_Right)
count if Pol_Right==.
drop if Pol_Right==.

// Drop respondents who did not consent to participate
count if Samtycke==2
drop if Samtycke==2

// Drop respondents missing policy variable (technical issues; 4 cases)
count if policy==.
drop if policy==.


// -------------------------------------------------------------
// SAVE CLEAN DATASET
// -------------------------------------------------------------
save "/Users/......./3_MainData_RuralResent.dta"
